// TechObjectSelectorWindow.xaml.cs
//
// Copyright (c) 2007 Mike Strobel
//
// This source code is subject to the terms of the Microsoft Reciprocal License (Ms-RL).
// For details, see <http://www.opensource.org/licenses/ms-rl.html>.
//
// All other rights reserved.

using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

using Supremacy.Tech;

namespace Supremacy.Editor
{
    /// <summary>
    /// Interaction logic for TechObjectSelectorWindow.xaml
    /// </summary>

    public partial class TechObjectSelectorWindow : Window
    {
        private IEnumerable<TechObjectDesign> _omissions;
        private List<TechObjectDesign> _items;

        public IList<TechObjectDesign> Items
        {
            get { return _items; }
        }

        public TechObjectSelectorWindow(IEnumerable<TechObjectDesign> omissions)
        {
            InitializeComponent();
            
            _omissions = omissions;

            ItemsList.SelectionChanged += ItemsList_SelectionChanged;

            SelectButton.Click += SelectButton_Click;
            SelectButton.IsEnabled = false;

            ItemsList.InputBindings.Add(new KeyBinding(ApplicationCommands.SelectAll, Key.A, ModifierKeys.Control));
            InputBindings.Add(new KeyBinding(ApplicationCommands.SelectAll, Key.A, ModifierKeys.Control));
            CommandBindings.Add(new CommandBinding(ApplicationCommands.SelectAll, SelectAll_Executed));

            LoadItems();
        }

        void SelectAll_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (ItemsList.SelectedItems.Count > 0)
            {
                ItemsList.SelectedItem = null;
            }
            else
            {
                ItemsList.SelectAll();
            }
        }

        public TechObjectSelectorWindow() : this(null) { }

        private void LoadItems()
        {
            TechDatabase db = EditorApp.Current.EditorContext.TechDatabase;
            List<TechObjectDesign> items = new List<TechObjectDesign>();

            foreach (TechObjectDesign design in db)
                items.Add(design);

            if (_omissions != null)
            {
                foreach (TechObjectDesign omission in _omissions)
                    items.Remove(omission);
            }

            _items = items;

            ItemsList.Items.SortDescriptions.Add(new SortDescription("Key", ListSortDirection.Ascending));
            ItemsList.ItemsSource = _items;
        }

        private void Item_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                ListViewItem item = e.Source as ListViewItem;
                TechObjectDesign design = ItemsList.ItemContainerGenerator.ItemFromContainer(item) as TechObjectDesign;
                if (design != null)
                {
                    ItemsList.SelectedItem = design;
                    DialogResult = true;
                }
            }
        }

        private void ItemsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectButton.IsEnabled = (ItemsList.SelectedItems.Count > 0);
        }

        private void SelectButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;   
        }

        public static TechObjectDesign GetSelection()
        {
            return GetSelection(null);            
        }

        public static IList<TechObjectDesign> GetSelections()
        {
            return GetSelections(null);
        }

        public static TechObjectDesign GetSelection(IEnumerable<TechObjectDesign> omissions)
        {
            TechObjectSelectorWindow dialog = new TechObjectSelectorWindow(omissions);
            dialog.Owner = EditorApp.Current.MainWindow;
            dialog.ShowDialog();
            if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                return dialog.ItemsList.SelectedItem as TechObjectDesign;
            return null;
        }

        public static IList<TechObjectDesign> GetSelections(IEnumerable<TechObjectDesign> omissions)
        {
            TechObjectSelectorWindow dialog = new TechObjectSelectorWindow(omissions);
            List<TechObjectDesign> items = new List<TechObjectDesign>();
            dialog.ItemsList.SelectionMode = SelectionMode.Multiple;
            dialog.Owner = EditorApp.Current.MainWindow;
            dialog.ShowDialog();
            if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
            {
                foreach (TechObjectDesign item in dialog.ItemsList.SelectedItems)
                    items.Add(item);
            }
            return items;
        }

        private void FilterText_TextChanged(object sender, TextChangedEventArgs e)
        {
            ItemsList.Items.Filter = (o => ((TechObjectDesign)o).Name.ToLowerInvariant().Contains(FilterText.Text.ToLowerInvariant()));
        }
    }
}